package com.fdhwws.web.servlet.api;

import com.fdhwws.web.domain.User;
import com.fdhwws.web.entity.AjaxResult;
import com.fdhwws.web.service.UserService;
import com.fdhwws.web.service.impl.UserServiceImpl;
import com.fdhwws.web.servlet.BaseServlet;
import com.fdhwws.web.util.CookieUtil;
import com.fdhwws.web.util.InfoSecurityUtil;
import jakarta.servlet.annotation.WebServlet;

import java.util.ArrayList;

@WebServlet(urlPatterns = "/api/user/*")
public class UserAPIServlet extends BaseServlet {
    private final UserService userService = new UserServiceImpl();

    public AjaxResult autoLogin(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyUser(httpUtil);
        if (ajaxResult.isSuccess()) {
            User data = (User) ajaxResult.data;
            return AjaxResult.success("自动登录成功", data.safeMode());
        }
        return ajaxResult;
    }

    public AjaxResult login(HttpUtil httpUtil) throws Exception {
        User jsonBody = httpUtil.getJSONBody(User.class);
        User login = userService.login(jsonBody.getUsername(), jsonBody.getPassword());
        if (login == null) {
            return AjaxResult.error("用户名或密码错误", null);
        } else {
            if (login.getValid()) {
                CookieUtil.setCookie(httpUtil.getResponse(), "uid", InfoSecurityUtil.encrypt(login.getId().toString()));
                return AjaxResult.success("登录成功", login.safeMode());
            }
            return AjaxResult.error("用户无效", null);
        }
    }

    public AjaxResult register(HttpUtil httpUtil) throws Exception {
        User jsonBody = httpUtil.getJSONBody(User.class);
        if (jsonBody.getUsername().trim().equals("系统消息")) {
            return AjaxResult.error("此用户名禁止注册", null);
        }
        jsonBody.setAdmin(false);
        jsonBody.setValid(true);
        return userService.register(jsonBody);
    }

    public AjaxResult logout(HttpUtil httpUtil) throws Exception {
        CookieUtil.deleteCookie(httpUtil.getResponse(), "uid");
        return AjaxResult.success("退出成功", null);
    }

    public AjaxResult verifyAdminUserApi(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyAdminUser(httpUtil);
        ajaxResult.data = null;
        return ajaxResult;
    }

    public AjaxResult getUserByUid_admin(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyAdminUser(httpUtil);
        if (!ajaxResult.isSuccess()) {
            return ajaxResult;
        }
        try {
            Number uid = httpUtil.getNumberParameter("uid");
            User userById = userService.getUserById(uid.intValue());
            if (userById == null) {
                return AjaxResult.error("用户不存在", null);
            } else {
                return AjaxResult.success("获取成功", userById);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return AjaxResult.error("uid异常", null);
    }

    public AjaxResult getUserList_admin(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyAdminUser(httpUtil);
        if (!ajaxResult.isSuccess()) {
            return ajaxResult;
        }
        return userService.getAllUser();
    }

    public AjaxResult updateUser_admin(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyAdminUser(httpUtil);
        if (!ajaxResult.isSuccess()) {
            return ajaxResult;
        }
        try {
            User jsonBody = httpUtil.getJSONBody(User.class);
            return userService.updateUser(jsonBody);
        } catch (Exception e) {
            return AjaxResult.error("数据异常", null);
        }
    }
}
